<?php
declare (strict_types = 1);

namespace app\middleware;

use app\api\service\Token;
use think\facade\Cache;

class CheckApi
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $token = $request->header('token');
        if ($token){
            $res = Token::checkToken($token);
            $user = Cache::get('user');
            if ($res['code'] == 2){
                return json(['code'=>300,'msg'=>'token错误']);
            }
            if ($res['data']->uid == $user['id'] ){
                $request->uid = $res['data']->uid;
                return $next($request);
            }
        }else{
            return json(['code'=>300,'msg'=>'非法访问，请登录']);
        }
        return $next($request);
    }
}
